﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RoadTeam.DAL;
using RoadTeam.Enums;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //dt.Text = DateTime.Now.ToString();
            //Strbool.Text = false.ToString();
        }
    }
    /// <summary>
    /// 判断是否可空类型
    /// </summary>
    /// <param name="theType"></param>
    /// <returns></returns>
    bool IsNullableType(Type T)
    {
        return (T.IsGenericType && T.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        model mo = new model();
        Control contr = this.Page;
        GetObjectFromPage(mo, contr);
        Response.Write(mo.Str);
    }

    protected void Server_Change(object Source, EventArgs e)
    {
        model mo = new model();
        Control contr = this.Page;
        GetObjectFromPage(mo, contr);
        Response.Write(mo.Str);
    }

    private object GetObjectFromPage(object t, Control Control)
    {
        foreach (System.Reflection.PropertyInfo p in t.GetType().GetProperties())
        {
            System.Reflection.PropertyInfo pinfo = t.GetType().GetProperty(p.Name);
            object value = new object();
            Returnfactory.factory(p.PropertyType).ForeachControl(p.Name, Control, ref value);
            if (!IsNullableType(p.GetType()) && value == null) continue;
            pinfo.SetValue(t, value, null);
        }
        return t;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //bool int float double decimal
        Response.Write("<P>" + typeof(bool).ToString().ToUpper() + "</P>");
        Response.Write("<P>" + typeof(int).ToString().ToUpper() + "</P>");
        Response.Write("<P>" + typeof(float).ToString().ToUpper() + "</P>");
        Response.Write("<P>" + typeof(double).ToString().ToUpper() + "</P>");
        Response.Write("<P>" + typeof(decimal).ToString().ToUpper() + "</P>");
    }
}

#region 返回值处理
public class Returnfactory
{
    public static ForeachControlBase factory(Type T)
    {
        ForeachControlBase ForeachControlBase;
        switch (T.ToString().ToUpper())
        {
            case "SYSTEM.DATETIME":
                ForeachControlBase = new ReturnDate();
                break;
            case "SYSTEM.STRING":
                ForeachControlBase = new ReturnString();
                break;
            case "SYSTEM.BOOLEAN":
                ForeachControlBase = new ReturnBool();
                break;
            case "SYSTEM.INT32":
                ForeachControlBase = new ReturnInt32();
                break;
            case "SYSTEM.SINGLE":
                ForeachControlBase = new ReturnSingle();
                break;
            case "SYSTEM.DOUBLE":
                ForeachControlBase = new ReturnDouble();
                break;
            case "SYSTEM.DECIMAL":
                ForeachControlBase = new ReturnDecimal();
                break;
            default:
                ForeachControlBase = new ForeachControlBase();
                break;
        }
        return ForeachControlBase;
    }
}
public class ForeachControlBase
{
    public virtual void ForeachControl(String constrolname, Control contr, ref object value)
    {
        value = null;
    }
}
class ReturnBool : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        bool flag = false;
        bool.TryParse(StrValue, out flag);
        value = false;
    }
}
class ReturnDate : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        DateTime dt = DateTime.MinValue;
        DateTime.TryParse(StrValue, out dt);
        if (dt == DateTime.MinValue) value = null;
        else value = Convert.ToDateTime(StrValue);
    }
}
class ReturnInt32 : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        if (String.IsNullOrEmpty(StrValue)) value = null;
        int Num = int.MinValue;

        int.TryParse(StrValue, out Num);
        if (Num == int.MinValue) value = null;
        else value = Num;
    }
}
class ReturnString : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        value = StrValue;
    }
}
class ReturnSingle : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        if (String.IsNullOrEmpty(StrValue)) value = null;
        Single single = Single.MinValue;
        Single.TryParse(StrValue, out single);
        if (single == Single.MinValue) value = null;
        else value = single;
    }
}
class ReturnDouble : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        if (String.IsNullOrEmpty(StrValue)) value = null;
        double doublevalue = double.MinValue;
        double.TryParse(StrValue, out doublevalue);
        if (doublevalue == double.MinValue) value = null;
        else value = doublevalue;
    }
}
class ReturnDecimal : ForeachControlBase
{
    public override void ForeachControl(String constrolname, Control contr, ref object value)
    {
        String StrValue = String.Empty;
        ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
        if (String.IsNullOrEmpty(StrValue)) value = null;
        decimal decimalvalue = decimal.MinValue;
        decimal.TryParse(StrValue, out decimalvalue);

        if (decimalvalue == decimal.MinValue) value = null;
        else value = decimalvalue;
    }
}
#endregion

#region 遍历页面中的控件,并且返回值
class ForEachControl
{
    public static void GetConTrolVal(ref string StrValue, string constrolname, Control contr)
    {
        foreach (Control ct in contr.Controls)
        {
            if (ct.Controls.Count > 0) GetConTrolVal(ref  StrValue, constrolname, ct);
            if (ct.ID == constrolname)
            {
                String StrControlType = ct.GetType().ToString().ToUpper();
                switch (StrControlType)
                {
                    case "SYSTEM.WEB.UI.WEBCONTROLS.TEXTBOX"://TextBox
                        StrValue = ((TextBox)ct).Text;
                        break;
                    case "SYSTEM.WEB.UI.WEBCONTROLS.RADIOBUTTONLIST"://单选
                        RadioButtonList radiolist = ((RadioButtonList)ct);
                        foreach (ListItem li in radiolist.Items)
                        {
                            if (li.Selected){StrValue = li.Value; break;}
                        }
                        break;
                    //case "SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOXLIST"://多项
                    //    break;
                    case "SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOX": //单选CHECKBOX
                        StrValue = ((CheckBox)ct).Checked.ToString();
                        break;
                    case "SYSTEM.WEB.UI.WEBCONTROLS.DROPDOWNLIST"://下拉
                        DropDownList ddl = ((DropDownList)ct);
                        if (ddl.Items.Count == 0 || ddl.SelectedValue == null || String.IsNullOrEmpty(ddl.SelectedValue))
                            StrValue = String.Empty;
                        else
                            StrValue = ddl.SelectedValue;
                        break;
                    case "SYSTEM.WEB.UI.WEBCONTROLS.LISTBOX"://列表
                        ListBox libox = (ListBox)ct;
                        if (libox.Items.Count == 0 || libox.SelectedValue == null || String.IsNullOrEmpty(libox.SelectedValue))
                            StrValue = String.Empty;
                        else
                            StrValue = libox.SelectedValue;
                        break;
                    case "SYSTEM.WEB.UI.WEBCONTROLS.LABEL"://标签
                        StrValue = ((Label)ct).Text;
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTRADIOBUTTON": //单选
                        StrValue = ((System.Web.UI.HtmlControls.HtmlInputRadioButton)ct).Checked.ToString();
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTCHECKBOX"://多选
                        System.Web.UI.HtmlControls.HtmlInputCheckBox chb = (HtmlInputCheckBox)ct;
                        StrValue = chb.Checked.ToString();
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTHIDDEN"://隐藏
                        System.Web.UI.HtmlControls.HtmlInputHidden hidden = (HtmlInputHidden)ct;
                        StrValue = hidden.Value.ToString();
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLINPUTTEXT"://单行
                        System.Web.UI.HtmlControls.HtmlInputText inptxt = (HtmlInputText)ct;
                        StrValue = inptxt.Value.ToString();
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLTEXTAREA"://多行
                        System.Web.UI.HtmlControls.HtmlTextArea inptxtArea = (HtmlTextArea)ct;
                        StrValue = inptxtArea.Value.ToString();
                        break;
                    case "SYSTEM.WEB.UI.HTMLCONTROLS.HTMLSELECT"://列表
                        System.Web.UI.HtmlControls.HtmlSelect select = (HtmlSelect)ct;
                        if (select.SelectedIndex > -1)
                            StrValue = select.Items[select.SelectedIndex].Value;
                        else
                            StrValue = String.Empty;
                        break;
                    default:
                        StrValue = String.Empty;
                        break;
                }
            }
        }
    }
}
#endregion


class model
{
    private string _str;
    public string Str
    {
        get { return _str; }
        set { _str = value; }
    }
}


